﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Election.MakePeaceKingdomDecision
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.BarterSystem.Barterables;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.MapNotificationTypes;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Election
{
  public class MakePeaceKingdomDecision : KingdomDecision
  {
    [SaveableField(101)]
    public readonly IFaction FactionToMakePeaceWith;
    [SaveableField(103)]
    private readonly bool _applyResults;
    [SaveableField(110)]
    public readonly int DailyTributeToBePaid;

    internal static void AutoGeneratedStaticCollectObjectsMakePeaceKingdomDecision(
      object o,
      List<object> collectedObjects)
    {
      ((KingdomDecision) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.FactionToMakePeaceWith);
    }

    internal static object AutoGeneratedGetMemberValueFactionToMakePeaceWith(object o)
    {
      return (object) ((MakePeaceKingdomDecision) o).FactionToMakePeaceWith;
    }

    internal static object AutoGeneratedGetMemberValueDailyTributeToBePaid(object o)
    {
      return (object) ((MakePeaceKingdomDecision) o).DailyTributeToBePaid;
    }

    internal static object AutoGeneratedGetMemberValue_applyResults(object o)
    {
      return (object) ((MakePeaceKingdomDecision) o)._applyResults;
    }

    public MakePeaceKingdomDecision(
      Clan proposerClan,
      IFaction kingdomToMakePeaceWith,
      int dailyTributeToBePaid = 0,
      bool applyResults = true)
      : base(proposerClan)
    {
      this.FactionToMakePeaceWith = kingdomToMakePeaceWith;
      this.DailyTributeToBePaid = dailyTributeToBePaid;
      this._applyResults = applyResults;
    }

    public override bool IsAllowed()
    {
      return Campaign.Current.Models.KingdomDecisionPermissionModel.IsPeaceDecisionAllowedBetweenKingdoms(this.Kingdom, this.FactionToMakePeaceWith as Kingdom, out TextObject _);
    }

    public override int GetProposalInfluenceCost()
    {
      return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfProposingPeace(this.ProposerClan);
    }

    public override TextObject GetGeneralTitle()
    {
      TextObject generalTitle = new TextObject("{=v3xdiFfD}Make Peace With {KINGDOM_NAME}");
      generalTitle.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.Name);
      return generalTitle;
    }

    public override TextObject GetSupportTitle()
    {
      TextObject supportTitle = this.DailyTributeToBePaid != 0 ? (this.DailyTributeToBePaid <= 0 ? new TextObject("{=NjPMRWbW}Make peace with the {KINGDOM_NAME}. Our kingdom will receive {T} tribute daily.") : new TextObject("{=2b1xZGaQ}Make peace with the {KINGDOM_NAME}. Our kingdom will pay {T} tribute daily.")) : new TextObject("{=0aXG8dvJ}Make peace with the {KINGDOM_NAME}. No tribute will be paid.");
      supportTitle.SetTextVariable("T", MathF.Abs(this.DailyTributeToBePaid));
      supportTitle.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
      return supportTitle;
    }

    public override TextObject GetChooseTitle()
    {
      TextObject chooseTitle = new TextObject("{=4GgfFDWG}Making peace with the {KINGDOM_NAME}");
      chooseTitle.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
      return chooseTitle;
    }

    public override TextObject GetSupportDescription()
    {
      TextObject empty = TextObject.Empty;
      TextObject supportDescription;
      if (this.DailyTributeToBePaid != 0)
      {
        supportDescription = new TextObject("{=BlVjIlZF}{FACTION_LEADER} will decide if the {KINGDOM_CONSIDERING_PEACE_NAME} will make peace with the {KINGDOM_NAME} by paying {TRIBUTE_PERCENTAGE} percent of the kingdom's income as tribute. You can pick your stance regarding this decision.");
        supportDescription.SetTextVariable("T", this.DailyTributeToBePaid);
      }
      else
        supportDescription = new TextObject("{=awoeb3br}{FACTION_LEADER} will decide if the {KINGDOM_CONSIDERING_PEACE_NAME} will make peace with the {KINGDOM_NAME}. You can pick your stance regarding this decision.");
      supportDescription.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
      supportDescription.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
      supportDescription.SetTextVariable("KINGDOM_CONSIDERING_PEACE_NAME", this.Kingdom.InformalName);
      return supportDescription;
    }

    public override TextObject GetChooseDescription()
    {
      TextObject empty = TextObject.Empty;
      TextObject chooseDescription;
      if (this.DailyTributeToBePaid != 0)
      {
        chooseDescription = new TextObject("{=n4I3pWOn}As the ruler, you must decide if peace will be made with the {KINGDOM_NAME} by paying {TRIBUTE_PERCENTAGE} percent of the kingdom's income as tribute.");
        chooseDescription.SetTextVariable("TRIBUTE_PERCENTAGE", this.DailyTributeToBePaid);
      }
      else
        chooseDescription = new TextObject("{=KFHj7ckm}As the ruler, you must decide if peace will be made with the {KINGDOM_NAME}");
      chooseDescription.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
      chooseDescription.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
      return chooseDescription;
    }

    public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
    {
      MakePeaceKingdomDecision peaceKingdomDecision = this;
      yield return (DecisionOutcome) new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(true, peaceKingdomDecision.Kingdom, peaceKingdomDecision.FactionToMakePeaceWith);
      yield return (DecisionOutcome) new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(false, peaceKingdomDecision.Kingdom, peaceKingdomDecision.FactionToMakePeaceWith);
    }

    public override Clan DetermineChooser() => this.Kingdom.RulingClan;

    protected override bool ShouldBeCancelledInternal()
    {
      return !this.Kingdom.IsAtWarWith(this.FactionToMakePeaceWith);
    }

    public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      foreach (DecisionOutcome possibleOutcome in (List<DecisionOutcome>) possibleOutcomes)
      {
        if (((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) possibleOutcome).ShouldPeaceBeDeclared)
          possibleOutcome.SetSponsor(this.ProposerClan);
        else
          this.AssignDefaultSponsor(possibleOutcome);
      }
    }

    public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
    {
      if (!this._applyResults || !((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) chosenOutcome).ShouldPeaceBeDeclared)
        return;
      MakePeaceAction.ApplyByKingdomDecision((IFaction) this.Kingdom, this.FactionToMakePeaceWith, this.DailyTributeToBePaid);
    }

    public override bool OnShowDecision()
    {
      if (this.FactionToMakePeaceWith != Clan.PlayerClan.Kingdom || Hero.MainHero.Clan.IsUnderMercenaryService)
        return true;
      if (!Hero.MainHero.IsPrisoner)
      {
        TextObject descriptionText = new TextObject("{=1V8f9vRM}A courier bearing a peace offer from the {PROPOSER_HERO_FACTION} has arrived at the court of your realm.");
        descriptionText.SetTextVariable("PROPOSER_HERO_FACTION", this.ProposerClan.Leader.MapFaction.InformalName);
        Campaign.Current.CampaignInformationManager.NewMapNoticeAdded((InformationData) new PeaceOfferMapNotification(this.ProposerClan.MapFaction, this.DailyTributeToBePaid, descriptionText));
      }
      return false;
    }

    public override TextObject GetSecondaryEffects()
    {
      return new TextObject("{=!}All supporters gains some relation with each other.");
    }

    public override void ApplySecondaryEffects(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes,
      DecisionOutcome chosenOutcome)
    {
      int num = this._applyResults ? 1 : 0;
    }

    public override TextObject GetChosenOutcomeText(
      DecisionOutcome chosenOutcome,
      KingdomDecision.SupportStatus supportStatus,
      bool isShortVersion = false)
    {
      TextObject parent;
      if (((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) chosenOutcome).ShouldPeaceBeDeclared)
      {
        if (this.IsSingleClanDecision())
        {
          parent = new TextObject("{=CswzBb02}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM}.");
        }
        else
        {
          switch (supportStatus)
          {
            case KingdomDecision.SupportStatus.Majority:
              parent = new TextObject("{=17A2DDgD}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM} with the support of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.");
              break;
            case KingdomDecision.SupportStatus.Minority:
              parent = new TextObject("{=JDfnPFsW}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM} despite the opposition of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.");
              break;
            default:
              parent = new TextObject("{=aEt1kqxm}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM}, with his {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council evenly divided on the matter.");
              break;
          }
        }
      }
      else if (this.IsSingleClanDecision())
      {
        parent = new TextObject("{=wsDNxArW}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM}.");
      }
      else
      {
        switch (supportStatus)
        {
          case KingdomDecision.SupportStatus.Majority:
            parent = new TextObject("{=mRrYn2qm}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM} with the support of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.");
            break;
          case KingdomDecision.SupportStatus.Minority:
            parent = new TextObject("{=Ing5gFbO}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM} over the objections of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.");
            break;
          default:
            parent = new TextObject("{=AThZtg7U}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided against making peace with the {KINGDOM}, with his {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council evenly divided on the matter.");
            break;
        }
      }
      StringHelpers.SetCharacterProperties("PEACEMAKER_RULER", this.Kingdom.Leader.CharacterObject, parent);
      parent.SetTextVariable("KINGDOM_CONSIDERING_PEACE", this.Kingdom.InformalName);
      parent.SetTextVariable("KINGDOM", this.FactionToMakePeaceWith.InformalName);
      return parent;
    }

    public override DecisionOutcome GetQueriedDecisionOutcome(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      return possibleOutcomes.FirstOrDefault<DecisionOutcome>((Func<DecisionOutcome, bool>) (t => ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) t).ShouldPeaceBeDeclared));
    }

    public float CalculateSupport(Clan clan)
    {
      return this.DetermineSupport(clan, (DecisionOutcome) new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(true, this.Kingdom, this.FactionToMakePeaceWith));
    }

    public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
    {
      MakePeaceKingdomDecision.MakePeaceDecisionOutcome peaceDecisionOutcome = (MakePeaceKingdomDecision.MakePeaceDecisionOutcome) possibleOutcome;
      int valueForFaction = new PeaceBarterable((IFaction) this.Kingdom, this.FactionToMakePeaceWith, CampaignTime.Years(1f)).GetValueForFaction((IFaction) clan);
      float num1 = clan.Leader != null ? (clan.Leader.Gold < 50000 ? (float) (1.0 + 0.5 * ((50000.0 - (double) clan.Leader.Gold) / 50000.0)) : (clan.Leader.Gold > 200000 ? MathF.Max(0.66f, MathF.Pow(200000f / (float) clan.Leader.Gold, 0.4f)) : 1f)) : 1f;
      int traitLevel = clan.Leader.GetTraitLevel(DefaultTraits.Generosity);
      float num2 = this.DailyTributeToBePaid > 0 ? (float) (1.0 - 0.10000000149011612 * (double) MathF.Max(-2, MathF.Min(2, traitLevel))) : 1f;
      int num3 = (int) ((double) Campaign.Current.Models.DiplomacyModel.GetValueOfDailyTribute(this.DailyTributeToBePaid) * (double) num1 * (double) num2);
      float num4 = (float) (valueForFaction - num3) * Campaign.Current.Models.DiplomacyModel.DenarsToInfluence();
      return peaceDecisionOutcome.ShouldPeaceBeDeclared ? num4 + (float) (clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10) : -num4 + (float) (-clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10);
    }

    public class MakePeaceDecisionOutcome : DecisionOutcome
    {
      [SaveableField(100)]
      public readonly bool ShouldPeaceBeDeclared;
      [SaveableField(110)]
      public readonly Kingdom Kingdom;
      [SaveableField(120)]
      public readonly IFaction FactionToMakePeaceWith;

      internal static void AutoGeneratedStaticCollectObjectsMakePeaceDecisionOutcome(
        object o,
        List<object> collectedObjects)
      {
        ((DecisionOutcome) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
      {
        base.AutoGeneratedInstanceCollectObjects(collectedObjects);
        collectedObjects.Add((object) this.Kingdom);
        collectedObjects.Add((object) this.FactionToMakePeaceWith);
      }

      internal static object AutoGeneratedGetMemberValueShouldPeaceBeDeclared(object o)
      {
        return (object) ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) o).ShouldPeaceBeDeclared;
      }

      internal static object AutoGeneratedGetMemberValueKingdom(object o)
      {
        return (object) ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) o).Kingdom;
      }

      internal static object AutoGeneratedGetMemberValueFactionToMakePeaceWith(object o)
      {
        return (object) ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome) o).FactionToMakePeaceWith;
      }

      public MakePeaceDecisionOutcome(
        bool shouldPeaceBeDeclared,
        Kingdom kingdom,
        IFaction factionToMakePeaceWith)
      {
        this.ShouldPeaceBeDeclared = shouldPeaceBeDeclared;
        this.Kingdom = kingdom;
        this.FactionToMakePeaceWith = factionToMakePeaceWith;
      }

      public override TextObject GetDecisionTitle()
      {
        TextObject decisionTitle = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}");
        decisionTitle.SetTextVariable("SUPPORT", this.ShouldPeaceBeDeclared ? 1 : 0);
        return decisionTitle;
      }

      public override TextObject GetDecisionDescription()
      {
        if (this.SponsorClan != null && this.Kingdom != null && this.FactionToMakePeaceWith != null && this.SponsorClan != Clan.PlayerClan)
        {
          TextObject reason = TextObject.Empty;
          if (this.ShouldPeaceBeDeclared)
          {
            double ofDeclaringPeace = (double) Campaign.Current.Models.DiplomacyModel.GetScoreOfDeclaringPeace((IFaction) this.Kingdom, this.FactionToMakePeaceWith, (IFaction) this.SponsorClan, out reason);
          }
          if (reason != TextObject.Empty)
            return reason;
        }
        return this.ShouldPeaceBeDeclared ? new TextObject("{=THz06NQD}It is time to make peace") : new TextObject("{=jQpeuHIE}We oppose making peace at this time");
      }

      public override string GetDecisionLink() => (string) null;

      public override ImageIdentifier GetDecisionImageIdentifier() => (ImageIdentifier) null;
    }
  }
}
